package com.haker.mensa2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.haker.mensa2.WGet.AException;



import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TUHHMensa extends Activity {
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		hContext = this;
        
		setRes(hContext);
        
		WGet.prepareUserAgent(hContext);
		String muh="";
		try {
			muh= WGet.getUrlContent("http://www.studierendenwerk-hamburg.de/essen/woche.php?haus=Harburg");
		} catch (AException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		setContentView(R.layout.test);
		
		final List<String> essen = Mensa.format(muh);
		for (int i=12; i<essen.size()-5;i++){
			System.out.println(i+":"+essen.get(i).length()+":"+essen.get(i));
			
		}
		//final String[] sup=suppen(muh);
		Time today = new Time();
        today.setToNow();
      
        System.out.println(today.weekDay);
        
		final ToggleButton zusatz = (ToggleButton)findViewById(R.id.zusatz);
		
		
		
		
		
		
		
		
		 //setEssen( setday(today.weekDay), sup,text1,text2);
		setday(today.weekDay);
		 menuBauen(essen,0);
		 
		final ListView list = (ListView) findViewById(R.id.listview);
		 
		ArrayList<HashMap<String, String>> mylist = listfuellen(liststring);
		SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listview,  new String[] {"a", "b"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL});
	  list.setAdapter(mSchedule);
	  list.setVisibility(View.GONE);
	  System.out.println(choise);
		
		
		
		
		
		zusatz.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
            	if (zusatz.isChecked()) {
            		list.setVisibility(View.VISIBLE);
            		
            		System.out.println("true");
            	} else {
            		list.setVisibility(View.GONE);
            		System.out.println("false");
            	}
            }
		});
            final Button vor = (Button)findViewById(R.id.vor);
            vor.setOnClickListener(new View.OnClickListener() { 
                public void onClick(View view) { 
                	menuBauen(essen,-1);                	
                } 
            
       });
            final Button nae = (Button)findViewById(R.id.nae);
            nae.setOnClickListener(new View.OnClickListener() { 
                public void onClick(View view) { 
                	menuBauen(essen,1);                	
                } 
            
       });
            
            final Button zu = (Button)findViewById(R.id.zu);
            zu.setOnClickListener(new View.OnClickListener() { 
                public void onClick(View view) { 
                	finish();                	
                } 
            
       });
		
        	
	}

	public void menuBauen(List<String> liste, int day){
		if (choise + day <7 && choise +day > -1){
			System.out.println(choise+day);
			choise =choise+day;
			
		
		int anzahl= liste.size()-17;
		TableLayout lay = (TableLayout)findViewById(R.id.lay);
		lay.removeAllViews();
		LayoutInflater layoutInflater = (LayoutInflater) hContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (anzahl/11==1)  {
			TableRow row = (TableRow)layoutInflater.inflate(R.layout.reihe3, null);
			TextView spalte1 =(TextView)row.getChildAt(0);
			spalte1.setText(liste.get(13));
			if(choise < 5){
				LinearLayout ll = (LinearLayout) row.getChildAt(1);
				TextView spalte2 = (TextView) ll.getChildAt(0);
				spalte2.setText(liste.get(13 + choise +1));
				TextView preis = (TextView) ll.getChildAt(1);
				preis.setText(liste.get(13 + choise + 2));
			}
	        lay.addView(row);			
		}
		else if (anzahl/11==2) {
			TableRow row1 = (TableRow)layoutInflater.inflate(R.layout.reihe1, null);
			TextView spalte1_1 =(TextView)row1.getChildAt(0);
			spalte1_1.setText(liste.get(13));
			if(choise < 5){
				LinearLayout ll1 = (LinearLayout) row1.getChildAt(1);
				TextView spalte1_2 = (TextView) ll1.getChildAt(0);
				spalte1_2.setText(liste.get(13 + choise +1));
				TextView preis1 = (TextView) ll1.getChildAt(1);
				preis1.setText(liste.get(13 + choise + 2));
			}
			lay.addView(row1);
	        
	        TableRow row2 = (TableRow)layoutInflater.inflate(R.layout.reihe3, null);
			TextView spalte2_1 =(TextView)row2.getChildAt(0);
			spalte2_1.setText(liste.get(24));
			if(choise < 5){
				LinearLayout ll2 = (LinearLayout) row2.getChildAt(1);
				TextView spalte2_2 = (TextView) ll2.getChildAt(0);
				spalte2_2.setText(liste.get(24 +choise + 1));
				TextView preis2 = (TextView) ll2.getChildAt(1);
				preis2.setText(liste.get(24 + choise + 2));
			}
	        lay.addView(row2);
		} 
		else if(((anzahl/11)%6==0)&&(anzahl/11>2)) {
			TableRow row = (TableRow)layoutInflater.inflate(R.layout.reihe1, null);
			TextView spalte1 =(TextView)row.getChildAt(0);
			spalte1.setText(liste.get(13));
			if(choise < 5){
				LinearLayout ll = (LinearLayout) row.getChildAt(1);
				TextView spalte2 = (TextView) ll.getChildAt(0);
				spalte2.setText(liste.get(13 + 2*choise+1));
				TextView preis = (TextView) ll.getChildAt(1);
				preis.setText(liste.get(13 + 2*choise + 2));
			}
	        lay.addView(row);
	        int i;
	        for (i=1;i<anzahl/11-1;i++){
	        	TableRow row2 = (TableRow)layoutInflater.inflate(R.layout.reihe2, null);
				TextView spalte2_1 =(TextView)row2.getChildAt(0);
				spalte2_1.setText(liste.get(13+11*i));
				if(choise < 5){
					LinearLayout ll2 = (LinearLayout) row2.getChildAt(1);
					TextView spalte2_2 = (TextView) ll2.getChildAt(0);
					spalte2_2.setText(liste.get(13 + 11 * i + 2*choise +1));
					TextView preis1 = (TextView) ll2.getChildAt(1);
					preis1.setText(liste.get(13 + 11 * i + 2*choise + 2));
				}
		        lay.addView(row2);
	        }
	        TableRow row2 = (TableRow)layoutInflater.inflate(R.layout.reihe3, null);
			TextView spalte2_1 =(TextView)row2.getChildAt(0);
			spalte2_1.setText(liste.get(13+11*i));
			if(choise < 5){
				LinearLayout ll2 = (LinearLayout) row2.getChildAt(1);
				TextView spalte2_2 = (TextView) ll2.getChildAt(0);
				spalte2_2.setText(liste.get(13 + 11 *i+ 2*choise + 1));
				TextView preis2 = (TextView) ll2.getChildAt(1);
				preis2.setText(liste.get(13 + 11 * i + 2*choise + 2));
			}
	        lay.addView(row2);
		}
		}
		
		
		
	}
	public int setday(int day ) {
		if( day == 0){
			choise = 6;
		} else {
			choise = day -1;
		}
		return choise;
	}
	
//	public String[] suppen(String text) {
//		String[] list = new String[72];
//		int a=text.indexOf("torange\"></d");
//
//		for (int times =0; times < 36; times++) {
//			int b=text.indexOf("tkl\">",a);
//			if(text.regionMatches(b+5, "&nbsp;", 0,6 )) {
//				list[times] ="";
//				list[times+36]="";
//			} else {
//				if (times%6==0||times%6==6){
//					list[times] ="";
//					list[times+36]="";
//				} else {
//					int c=text.indexOf("</b><",b+5);
//					list[times]=umlaute_weg(text.substring(b+11,c));
//					int d=text.indexOf("ge\">", b+5);
//					list[times+36]=text.substring(d+4,d+4+11);
//					System.out.println(list[times]+" "+list[times+36]);
//				}
//			}
//			a=b+5;
//		}
//		return list;
//	}

//	public String umlaute_weg(String text) {
//		String tmp = text;
//		for(int i = 0; i < 7; i++) {
//			tmp=tmp.replace(umlaute_html[i], umlaute_plain[i]);
//		}
//		return tmp;
//	}
	public ArrayList<HashMap<String, String>> listfuellen(String[] string1) {
		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		for (int i=0; i < string1.length; i++) {
			map.put("a", new Integer(i+1).toString());
			map.put("b", string1[i]);
			mylist.add(map);
			map = new HashMap<String, String>();
		}
		
		return mylist;
		
	}
	 
	public void setRes(Context content) {
		Resources res = content.getResources();
		umlaute_html = res.getStringArray(R.array.umlaute_html);
		umlaute_plain = res.getStringArray(R.array.umlaute_plain);
		wochentage =res.getStringArray(R.array.daynames);
		liststring=res.getStringArray(R.array.zusatzstoffe);
		
		
		
		
		
	}
	
	private String[] umlaute_html; 
	private String[] umlaute_plain;
	private String[] wochentage;
	private String[] liststring;
	
	private int choise;
	private Context hContext;
	
}

